package greentycoon.gui;

import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;


public class SplashScreen extends JFrame{
	

	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;




	public SplashScreen(){
		setLayout(null);
		ImageIcon mainImage = new ImageIcon("src/greentycoon/gui/splashScreen.png");
		int sizeX = mainImage.getIconWidth();
		int sizeY = mainImage.getIconHeight();
		
		Toolkit t = getToolkit(); 
		Dimension d = t.getScreenSize(); 
		setSize(sizeX, sizeY);
		setUndecorated(true);
		setLocation((d.width-sizeX)/2,(d.height-sizeY)/2);
		JLabel mainLabel = new JLabel(mainImage);
		mainLabel.setBounds(0, 0, mainImage.getIconWidth(), mainImage.getIconHeight());
		add(mainLabel);
	
	}
	
	
	
	
	public static ImageIcon rescaleImage(File source,int maxHeight, int maxWidth)
	{
	    int newHeight = 0, newWidth = 0;        // Variables for the new height and width
	    int priorHeight = 0, priorWidth = 0;
	    BufferedImage image = null;
	    ImageIcon sizeImage;

	    try {
	            image = ImageIO.read(source);        // get the image
	    } catch (Exception e) {

	            e.printStackTrace();
	            System.out.println("Picture upload attempted & failed");
	    }

	    sizeImage = new ImageIcon(image);

	    if(sizeImage != null)
	    {
	        priorHeight = sizeImage.getIconHeight(); 
	        priorWidth = sizeImage.getIconWidth();
	    }

	    // Calculate the correct new height and width
	    if((float)priorHeight/(float)priorWidth > (float)maxHeight/(float)maxWidth)
	    {
	        newHeight = maxHeight;
	        newWidth = (int)(((float)priorWidth/(float)priorHeight)*(float)newHeight);
	    }
	    else 
	    {
	        newWidth = maxWidth;
	        newHeight = (int)(((float)priorHeight/(float)priorWidth)*(float)newWidth);
	    }


	    // Resize the image

	    // 1. Create a new Buffered Image and Graphic2D object
	    BufferedImage resizedImg = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_ARGB_PRE);
	    Graphics2D g2 = resizedImg.createGraphics();

	    // 2. Use the Graphic object to draw a new image to the image in the buffer
	    g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
	    g2.drawImage(image, 0, 0, newWidth, newHeight, null);
	    g2.dispose();

	    // 3. Convert the buffered image into an ImageIcon for return
	    return (new ImageIcon(resizedImg));
	}


}
